package cate.game.activity.continuousrecharge;

import cate.common.table.activity.continuousrecharge.level.ContinuousRechargeLevelRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@NoteClass("连续充值-单档位信息")
public class ContinuousRechargeActivityLevelPO extends SubActivitySingle<ContinuousRechargeActivity> {
    @NoteField("档位")
    public int level;
    @NoteField("当前累计天数")
    public int day;
    @NoteField("今日是否已记录")
    public boolean gotToday;
    @NoteField("累计奖励是否已领取")
    public boolean addUpGot;

    public ContinuousRechargeActivityLevelPO(ContinuousRechargeActivity parent, int level) {
        this.level = level;
        initialize(parent);
    }

    public void clear() {
        day = 0;
        gotToday = false;
        addUpGot = false;
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        gotToday = false;
    }

    /**
     * 检测充值数量
     */
    public void checkPay() {
        // 今日已记录的就无视
        if (gotToday) {
            return;
        }
        ContinuousRechargeLevelRow config = parent.getLevelConfig(level);
        if (config == null) {
            return;
        }
        if (parent.paySum < config.rechargeSum) {
            return;
        }
        // 记录天数和今日已记录
        day++;
        gotToday = true;
    }

    /**
     * 领取累计奖励
     *
     * @return 领取结果 携带活动类
     */
    public GameResult<ContinuousRechargeActivity> onGetAddUpReward() {
        GameResult<ContinuousRechargeActivity> r = new GameResult<>();
        r.data = parent;
        if (addUpGot) {
            return r.fail("累计奖励已领取");
        }
        ContinuousRechargeLevelRow config = parent.getLevelConfig(level);
        if (config == null) {
            return r.fail("配置错误");
        }
        if (day < config.addUpTimes) {
            return r.fail("充值天数不足");
        }
        MixRes reward = new MixRes(config.addUpRewardStr);
        if (reward.isEmpty()) {
            return r.fail("奖励配错了");
        }
        addUpGot = true;
        EcResult exe = new MixResAdder().setRes(reward)
                .setDialogReward(true)
                .setSendMailWhenFull(true)
                .setOperation(parent.getAddUpOperation())
                .setOperationSub(level)
                .exe(role);
        return r.success();
    }
}
